const { PrismaClient } = require('@prisma/client')
const bcrypt = require('bcrypt')
const { uuidv7 } = require('uuidv7')

const prisma = new PrismaClient()

async function seed() {
  console.log('🌱 Seeding database...')
  
  try {
    // 创建示例用户
    const hashedPassword = await bcrypt.hash('password123', 10)
    
    const user1 = await prisma.user.upsert({
      where: { email: 'admin@example.com' },
      update: {},
      create: {
        id: uuidv7(),
        email: 'admin@example.com',
        username: 'admin',
        password: hashedPassword,
        nickname: '管理员',
        bio: '系统管理员账户'
      }
    })

    const user2 = await prisma.user.upsert({
      where: { email: 'user@example.com' },
      update: {},
      create: {
        id: uuidv7(),
        email: 'user@example.com',
        username: 'testuser',
        password: hashedPassword,
        nickname: '测试用户',
        bio: '这是一个测试用户账户'
      }
    })

    // 创建示例标签
    const tag1 = await prisma.tag.upsert({
      where: { name: 'JavaScript' },
      update: {},
      create: {
        id: uuidv7(),
        name: 'JavaScript',
        color: '#f7df1e'
      }
    })

    const tag2 = await prisma.tag.upsert({
      where: { name: 'Node.js' },
      update: {},
      create: {
        id: uuidv7(),
        name: 'Node.js',
        color: '#339933'
      }
    })

    const tag3 = await prisma.tag.upsert({
      where: { name: 'TypeScript' },
      update: {},
      create: {
        id: uuidv7(),
        name: 'TypeScript',
        color: '#3178c6'
      }
    })

    // 创建示例文章
    const post1 = await prisma.post.create({
      data: {
        id: uuidv7(),
        title: 'Welcome to Our Blog',
        content: `# Welcome to Our Blog

This is our first blog post! We're excited to share our thoughts and experiences with you.

## What you can expect

- Technical tutorials
- Industry insights
- Best practices
- And much more!

Stay tuned for more exciting content.`,
        summary: 'Welcome to our new blog platform! This is our first post introducing what you can expect.',
        status: 'PUBLISHED',
        views: 42,
        likes: 5,
        authorId: user1.id,
        tags: {
          create: [
            { tagId: tag1.id },
            { tagId: tag2.id }
          ]
        }
      }
    })

    const post2 = await prisma.post.create({
      data: {
        id: uuidv7(),
        title: 'Getting Started with Node.js and TypeScript',
        content: `# Getting Started with Node.js and TypeScript

TypeScript has become increasingly popular in the Node.js ecosystem. In this post, we'll explore how to set up a new Node.js project with TypeScript.

## Prerequisites

- Node.js installed on your system
- Basic knowledge of JavaScript
- Familiarity with npm/yarn

## Setting up the project

First, let's create a new directory and initialize our project:

\`\`\`bash
mkdir my-node-ts-project
cd my-node-ts-project
npm init -y
\`\`\`

Next, install TypeScript and other necessary dependencies:

\`\`\`bash
npm install -D typescript @types/node ts-node nodemon
npm install express
npm install -D @types/express
\`\`\`

## Configuration

Create a \`tsconfig.json\` file:

\`\`\`json
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "commonjs",
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true
  }
}
\`\`\`

That's it! You now have a basic Node.js project with TypeScript support.`,
        summary: 'Learn how to set up a new Node.js project with TypeScript support, including configuration and best practices.',
        status: 'PUBLISHED',
        views: 128,
        likes: 15,
        authorId: user2.id,
        tags: {
          create: [
            { tagId: tag2.id },
            { tagId: tag3.id }
          ]
        }
      }
    })

    // 创建示例评论
    const comment1 = await prisma.comment.create({
      data: {
        id: uuidv7(),
        content: 'Great post! Looking forward to more content like this.',
        postId: post1.id,
        authorId: user2.id
      }
    })

    await prisma.comment.create({
      data: {
        id: uuidv7(),
        content: 'Thank you! We have many more posts planned.',
        postId: post1.id,
        parentId: comment1.id,
        authorId: user1.id
      }
    })

    await prisma.comment.create({
      data: {
        id: uuidv7(),
        content: 'Very helpful tutorial! The setup process was straightforward.',
        postId: post2.id,
        authorId: user1.id
      }
    })

    console.log('✅ Database seeded successfully!')
    console.log('\nCreated users:')
    console.log('- admin@example.com (password: password123)')
    console.log('- user@example.com (password: password123)')
    console.log('\nCreated 2 posts with tags and comments')
    
  } catch (error) {
    console.error('❌ Error seeding database:', error)
    throw error
  } finally {
    await prisma.$disconnect()
  }
}

seed()
  .catch((error) => {
    console.error(error)
    process.exit(1)
  })
